package com.itheima.app.controller;

import com.itheima.app.manager.MovementManager;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.vo.MovementVo;
import com.itheima.domain.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.Map;

@RestController
public class MovementController {

    @Autowired
    private MovementManager movementManager;

    //发布动态
    @PostMapping("/movements")
    public Map saveMovement(Movement movement, MultipartFile[] imageContent) throws IOException {
        //调用manager 保存动态
        movementManager.saveMovement(movement, imageContent);
        return null;
    }

    //查询我的个人动态，还可以其他接口使用
    @GetMapping("/movements/all")
    public PageBeanVo findMyMovement(@RequestParam(defaultValue = "1", name = "page") Integer pageNumber, @RequestParam(defaultValue = "10", name = "pagesize") Integer pageSize, Long userId) {
        //查询数据
        //System.out.println("--------------" + movementManager.findMyMovement(pageNumber, pageSize, userId));
        return  movementManager.findMyMovement(pageNumber, pageSize, userId);
    }

    //查询好友动态
    @GetMapping("/movements")
    public PageBeanVo findFriendMovements(@RequestParam(defaultValue = "1", name = "page") Integer pageNumber, @RequestParam(defaultValue = "10", name = "pagesize") Integer pageSize) {

        return  movementManager.findFriendMovements(pageNumber, pageSize);
    }

    //推荐动态
    @GetMapping("/movements/recommend")
    public PageBeanVo findRecommendMovement(@RequestParam(defaultValue = "1", name = "page") Integer pageNumber, @RequestParam(defaultValue = "10", name = "pagesize") Integer pageSize) {

        return  movementManager.findRecommendMovement(pageNumber, pageSize);
    }


    //动态点赞
    @GetMapping("/movements/{id}/like")
    public Integer saveMovementLike(@PathVariable(value = "id")String movementId) {
        //查询数据
        return  movementManager.saveMovementLike(movementId);
    }

    //动态取消点赞
    @GetMapping("/movements/{id}/dislike")
    public Integer deleteMovementLike(@PathVariable(value = "id")String movementId) {
        //查询数据
        return  movementManager.deleteMovementLike(movementId);
    }

    //动态喜欢
    @GetMapping("/movements/{id}/love")
    public Integer saveMovementLove(@PathVariable(value = "id")String movementId) {
        //查询数据
        return  movementManager.saveMovementLove(movementId);
    }

    //动态取消喜欢
    @GetMapping("/movements/{id}/unlove")
    public Integer deleteMovementLove(@PathVariable(value = "id")String movementId) {
        //查询数据
        return  movementManager.deleteMovementLove(movementId);
    }

    //单条动态
    @GetMapping("/movements/{id}")
    public MovementVo findMovementById(@PathVariable(value = "id")String movementId) {
        //查询数据
        return  movementManager.findMovementById(movementId);
    }

    //评论列表
    @GetMapping("/comments")
    public PageBeanVo findMovementComment(@RequestParam(defaultValue = "1", name = "page") Integer pageNumber, @RequestParam(defaultValue = "10", name = "pagesize") Integer pageSize,String movementId) {
        //查询数据
        return  movementManager.findMovementComment(pageNumber,pageSize,movementId);
    }

    //发表评论
    @PostMapping("/comments")
    public Map saveMovementComment(@RequestBody Map<String,String> map) {
        //获取数据
        String movementId = map.get("movementId");
        String content = map.get("comment");
        //查询数据
        return movementManager.saveMovementComment(movementId, content);
    }

    //喜欢统计
    @GetMapping("/messages/loves")
    public PageBeanVo findLoveComment(@RequestParam(defaultValue = "1", name = "page") Integer pageNumber, @RequestParam(defaultValue = "10", name = "pagesize") Integer pageSize) {
        int commentType = 3;
        //查询数据
        return movementManager.findCommentByCommentType(pageNumber, pageSize, commentType);
    }

    //点赞统计
    @GetMapping("/messages/likes")
    public PageBeanVo findLikeComment(@RequestParam(defaultValue = "1", name = "page") Integer pageNumber, @RequestParam(defaultValue = "10", name = "pagesize") Integer pageSize) {
        int commentType = 1;
        //查询数据
        return movementManager.findCommentByCommentType(pageNumber, pageSize, commentType);
    }

    //点赞统计
    @GetMapping("/messages/comments")
    public PageBeanVo findComment(@RequestParam(defaultValue = "1", name = "page") Integer pageNumber, @RequestParam(defaultValue = "10", name = "pagesize") Integer pageSize) {
        int commentType = 2;
        //查询数据
        return movementManager.findCommentByCommentType(pageNumber, pageSize, commentType);
    }

    //公告信息查询
    @GetMapping("/messages/announcements")
    public PageBeanVo findAnnouncements(@RequestParam(defaultValue = "1", name = "page") Integer pageNumber, @RequestParam(defaultValue = "10", name = "pagesize") Integer pageSize) {

        //System.out.println("我被调用了");
        //公告信息查询
        return movementManager.findAnnouncements(pageNumber, pageSize);
    }
    @GetMapping("/comments/{id}/like")
    public Integer movementCommentLike(@PathVariable(value = "id")String commentId) {
        //查询数据
        return  movementManager.movementCommentLike(commentId);
    }
    @GetMapping("/comments/{id}/dislike")
    public Integer deleteMovementCommentLike(@PathVariable(value = "id")String commentId) {
        //查询数据
        return  movementManager.deleteMovementCommentLike(commentId);
    }

}
